 		
		
var userwebsocket = null;
//防止页面后退
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});

var showchatwindow=function(e){
     $(".GJchatlist").slideToggle();
     $(".small-chat-box").fadeOut();
}

$(".GJchatlist").on("click","div",function(){
    $(".small-chat-box").fadeOut();
    var targetsession=$(this).attr("value");
    $("#chatwindow"+targetsession).fadeIn();
    var thischatnum=$(this).next().html();
    var msgnumval=$(".msgnum").html();
    if(thischatnum!=""){
    	$(this).next().html("");
    	if(msgnumval==""){
    		
    	}else if(msgnumval=='0'){
    		$(".msgnum").html("");
    	}else{
    		if(parseInt(msgnumval)-parseInt(thischatnum)==0){
    			$(".msgnum").html("");
    		}else{
    			$(".msgnum").html(parseInt(msgnumval)-parseInt(thischatnum));
    		}
    	}
    }
})

$("#chatlistdiv").on("click","button",function(){
	var nowdate=new Date();
	var replymsg=$(this).parent().prev("input").val();
	if(replymsg!=""){
		$(this).parent().prev("input").val("");
		$(this).parent().parent().parent().prev(".GJchatContent").append("<div class='left'><div class='author-name'>我&nbsp;&nbsp;<small class='chat-date'>"+p(nowdate.getHours())+":"+p(nowdate.getMinutes())+"</small>"+
				"</div><div class='chat-message active'>"+replymsg+"</div></div>")
		userwebsocket.send($(this).parent().attr("value")+"^&*#"+replymsg);
	}else{
		alert("不允许发送空字符，请输入内容");
	}
	
})

function p(s) {
        return s < 10 ? '0' + s: s;
    }

$.ajax({ 
    type : "post",
    url :  url.server+param.bingadminwebstock,
    data : { 
    },
    success: function(res) {
    	 if (res.status == '1') {
    		 console.log("绑定webstock成功");
    		 if('WebSocket' in window){
    	            userwebsocket = new WebSocket(url.webstockserver+param.webstockaddress);
    	            
    	            //连接发生错误的回调方法
    	            userwebsocket.onerror = function(event){
    	            	showWebstockCon("在线咨询连接失败");
    	            }
    	             
    	            //连接成功建立的回调方法
    	            userwebsocket.onopen = function(event){
    	            	showWebstockCon("在线咨询连接成功");
    	            }
    	             
    	            //接收到消息的回调方法
    	            userwebsocket.onmessage = function(event){
    	            	var msgJson=$.parseJSON(event.data);
    	            	if(msgJson[0].status=='1'){
    	            		var nowdate=new Date();
    	            		var targetsession=msgJson[0].extra.targetsession;
    	            		if(!$("#targetchat"+msgJson[0].extra.targetsession).length > 0){
    	            			$(".GJchatlist").append("<div class='GJchatlistitem' value='"+targetsession+"' id='targetchat"+targetsession+"'>游客"+targetsession+"</div><span class='label label-danger' style='position:absolute;margin-top:-28px;margin-left:50px;'>1</span>");
    	            			//再添加窗口
    	            			$("#chatlistdiv").append("<div class='small-chat-box' id='chatwindow"+targetsession+"'><div class='heading' draggable='true'>"+
    	            					"<small class='chat-date pull-right'>"+nowdate.getFullYear()+"."+nowdate.getMonth()+1+"."+nowdate.getDate()+"</small> 与 游客"+targetsession+"聊天中"+
    	            					"</div><div class='content GJchatContent' id='userchatContent"+targetsession+"'>"+
    	            					"<div class='right'><div class='author-name'>游客"+targetsession+"&nbsp;<small class='chat-date'>"+p(nowdate.getHours())+":"+p(nowdate.getMinutes())+"</small>"+
    	            					"</div><div class='chat-message'>"+msgJson[0].extra.message+"</div></div></div><div class='form-chat'>"+
    	            					"<div class='input-group input-group-sm adminSendMsg'><input type='text' class='form-control'><span class='input-group-btn' value='"+targetsession+"'><button class='btn btn-primary' type='button'>发送</button> </span></div></div></div>");
    	            			var msgnumval=$(".msgnum").html();
    	            			if(msgnumval==""){
    	            				$(".msgnum").html("1");
    	            			}else{ 
    	            				$(".msgnum").html(parseInt(msgnumval)+1);
    	            			}
    	            		}else{
    	            			//添加信息
    	            			$("#userchatContent"+targetsession).append("<div class='right'><div class='author-name'>游客"+targetsession+"&nbsp;&nbsp;&nbsp;&nbsp;<small class='chat-date'>"+p(nowdate.getHours())+":"+p(nowdate.getMinutes())+"</small>"+
    	            					"</div><div class='chat-message'>"+msgJson[0].extra.message+"</div></div></div>");
    	            			$("#userchatContent"+targetsession).animate({scrollTop:($("#userchatContent"+targetsession).height()+$("#userchatContent"+targetsession).scrollTop())+'px'},500);
    	            			var msgnumval=$(".msgnum").html();
    	            			if(msgnumval==""){
    	            				$(".msgnum").html("1");
    	            			}else{ 
    	            				$(".msgnum").html(parseInt(msgnumval)+1);
    	            			}
    	            			var smsgnumval=$("#targetchat"+targetsession).next().html()
    	            			if(smsgnumval==""){
    	            				$("#targetchat"+targetsession).next().html("1");
    	            			}else{ 
    	            				$("#targetchat"+targetsession).next().html(parseInt(smsgnumval)+1);
    	            			}
    	            		}
    	            	}else if(msgJson[0].status=='2'){
    	            		var msgJson=$.parseJSON(event.data);
    	            		var chatnum=$("#targetchat"+msgJson[0].data).next().html();
    	            		$("#targetchat"+msgJson[0].data).remove();
    	            		$("#chatwindow"+msgJson[0].data).remove();
    	            		if(chatnum!=""){
	            				$(".msgnum").html(parseInt($(".msgnum").html())-chatnum);
	            			}
    	            	}
    	            }
    	             
    	            //连接关闭的回调方法
    	            userwebsocket.onclose = function(){
    	            	showWebstockCon("在线咨询连接关闭");
    	            }
    	             
    	            //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    	            window.onbeforeunload = function(){
    	                userwebsocket.close();
    	            }
    	        }
    	        else{
    	        	alert("对不起，您的浏览器不支持在线咨询，请更换浏览器");
    	        }
         }else if (res.status == '2') {
        	 console.log("绑定webstock失败");
        };
    } 
});


//将消息显示在网页上
function setMessageInnerHTML(innerHTML){
	showWebstockCon(innerHTML);
}
 
//关闭连接
function closeWebSocket(){
    userwebsocket.close();
}

function showWebstockCon(msg){
	msg="&nbsp;&nbsp;&nbsp;&nbsp;"+msg;
	$(".GJwebstockConmsg").animate({width:'150px'},500);
	$(".GJwebstockConmsg").html(msg);
	setTimeout(function () {
		$(".GJwebstockConmsg").html("");
		$(".GJwebstockConmsg").animate({width:'0px'},1000);
		$(".GJwebstockConmsg").attr("display","none");
	}, 2000);
}